<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="searchForm" ref="searchForm">
            <el-form-item prop="linkMan">
              <el-input v-model="searchForm.linkMan" clearable placeholder="联系人"></el-input>
            </el-form-item>
            <el-form-item prop="linkPhone">
              <el-input v-model="searchForm.linkPhone" clearable placeholder="联系电话"></el-input>
            </el-form-item>
            <el-form-item prop="collageId">
              <el-input v-model="searchForm.collageId" clearable placeholder="拼团号"></el-input>
            </el-form-item>
            <HospitalSelect ref="HospitalSelect" @hospitalId="getHospitalId" :site="2"></HospitalSelect>
            <!--<el-form-item prop="sampleType">
              <el-select v-model="searchForm.sampleType" :clearable="true" placeholder="请选择采样类型">
                <el-option v-for="item in sampleTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>-->
            <el-form-item prop="collageState">
              <el-select v-model="searchForm.collageState" :clearable="true" placeholder="请选择拼团状态">
                <el-option v-for="item in collageStateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="refundState">
              <el-select v-model="searchForm.refundState" :clearable="true" placeholder="请选择退款状态">
                <el-option v-for="item in refundStateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="onSearch">搜索</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="init" size="mini"></el-button>
          </el-tooltip>
          <!--<el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd"
            @click="handleClickAdd()" size="mini"> 新增</el-button>-->
          <!--<span class="line">|</span>-->
          <!--<el-button type="primary" icon="fa fa-mail-forward">导出</el-button>-->
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="tableList" @row-dblclick="handleClickDetail" :fit="true">
        <!--<el-table-column type="index" label="序号" width="50"></el-table-column>-->
        <el-table-column prop="collageId" label="拼团号" width="70" align="center"></el-table-column>
        <el-table-column prop="itemName" label="项目名称" width="140"></el-table-column>
        <el-table-column width="180" label="医院-科室-医生">
          <template slot-scope="scope">
            <span>{{ scope.row.hospitalName }} - {{ scope.row.officeName }} - {{ scope.row.doctorName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userMin" label="最少成团人数" width="70" align="center"></el-table-column>
        <el-table-column prop="userCount" label="人员总数" width="70" align="center"></el-table-column>
        <el-table-column prop="itemPrice" label="采样单价" width="70" align="center"></el-table-column>
        <el-table-column prop="applyTime" label="采样时间" width="120"></el-table-column>
        <el-table-column prop="endTime" label="截止时间" width="120"></el-table-column>
        <el-table-column prop="linkMan" label="联系人" width="100"></el-table-column>
        <el-table-column prop="linkPhone" label="联系电话" width="100"></el-table-column>
        <el-table-column prop="address" label="详细地址" width="200"></el-table-column>
        <el-table-column label="拼团状态" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatCollageStateType(scope.row.collageState)">
                {{ formatCollageState(scope.row.collageState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="140"></el-table-column>
        <el-table-column label="退款状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.refundState">
              <el-tag :type="formatRefundStateType(scope.row.refundState)" size="mini" effect="plain">
                {{ formatRefundState(scope.row.refundState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
              <el-dropdown trigger="click" size="small">
                <el-button type="primary"><i class="fa fa-list-ul"></i>更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <!--<el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                  <el-dropdown-item v-has="permissionUpUser" @click.native="handleClickEditUser(scope.row)">修改用户</el-dropdown-item>-->
                  <el-dropdown-item v-has="permissionViewPayer" @click.native="handleClickViewPayer(scope.row)">支付信息</el-dropdown-item>
                  <el-dropdown-item v-has="permissionUpStatus" @click.native="handleClickDelete(scope.row, 0)">{{ scope.row.state == 0 ? '启用' : '禁用' }}</el-dropdown-item>
                  <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state == 0" @click.native="handleClickDelete(scope.row, -1)">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <el-dialog title="支付信息" :visible.sync="hideDialogShow" :center="isCenter" width="1300px">
      <el-table :data="collagePayerList" border style="width: 100%">
        <el-table-column property="collageId" label="拼团号" width="70" align="center"></el-table-column>
        <el-table-column prop="userNum" label="人数" width="70" align="center"></el-table-column>
        <el-table-column prop="payAmount" label="支付金额" width="70" align="center"></el-table-column>
        <el-table-column prop="refundAmount" label="退款金额" width="70" align="center"></el-table-column>
        <el-table-column prop="realAmount" label="实付金额" width="70" align="center"></el-table-column>
        <el-table-column label="支付状态" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.payState">
              <el-tag :type="formatPayStateType(scope.row.payState)" size="mini" effect="plain">
                {{ formatPayState(scope.row.payState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column width="150" label="联系方式">
          <template slot-scope="scope">
            <span>{{ scope.row.linkMan }} - {{ scope.row.linkPhone }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="详细地址" width="200"></el-table-column>
        <el-table-column prop="isMyself" label="发起人" width="60" align="center" :formatter="formatIsMyself"></el-table-column>
        <el-table-column prop="payTime" label="支付时间" width="120"></el-table-column>
        <el-table-column prop="refundTime" label="退款时间" width="120"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="140"></el-table-column>
        <!--<el-table-column label="操作" width="100" >
          <template slot-scope="scope">
            <el-row>
              <el-button @click="updateSampleUser(scope.row)" size="mini">保存</el-button>
            </el-row>
          </template>
        </el-table-column>-->
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import CollageApi from '@/api/sample/collage';
import HospitalSelect from '@/components/HospitalSelect';

export default {
  name: 'CollageManagement', // 缓存当前页面，需要给当前页面命名，并且在路由meta加上cache:true
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      tableList: [],
      collagePayerList: [],
      hospitalNameList: [],
      hideHospitalForm: {
        institutionId: '',
        hospitalSelIds: [],
        institutionSelName: '',
      },
      hideDialogShow: false,
      isCenter: true,

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      searchForm: {},
      //1.单采 5.混采5人 10.混采10人
      sampleTypeList: [
        { label: '单采', value: 1 },
        { label: '5人混采', value: 5 },
        { label: '10人混采', value: 10 },
      ],
      //拼团状态：(1.拼团中 2.已成团 3.已完成 4.已取消)
      collageStateList: [
        { label: '拼团中', value: '1' },
        { label: '已成团', value: '2' },
        { label: '已完成', value: '3' },
        { label: '已取消', value: '4' },
      ],
      //退款状态：(1.待退款 2.已退款 3.待退差价 4.已退差价)
      refundStateList: [
        { label: '待退款', value: '1' },
        { label: '已退款', value: '2' },
        { label: '待退差价', value: '3' },
        { label: '已退差价', value: '4' },
      ],
      permissionCheck: 'SYSTEM_MANAGER_SAMPLE_COLLAGE_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_SAMPLE_COLLAGE_LIST_EDIT',
      permissionViewPayer: 'SYSTEM_MANAGER_SAMPLE_COLLAGE_LIST_VIEW_PAYER',
      permissionUpStatus: 'SYSTEM_MANAGER_SAMPLE_COLLAGE_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_SAMPLE_COLLAGE_LIST_DELETE',
    };
  },
  components: {
    HospitalSelect,
  },
  created() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  //activated,deactivated这两个生命周期函数一定是要在使用了keep-alive组件后才会有的，否则则不存在
  activated() {
    this.init();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listTable();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    //拼团状态：(1.拼团中 2.已成团 3.已完成 4.已取消)
    formatCollageState(cellValue) {
      if (cellValue == 1) {
        return '拼团中';
      } else if (cellValue == 2) {
        return '已成团';
      } else if (cellValue == 3) {
        return '已完成';
      } else if (cellValue == 4) {
        return '已取消';
      }
    },
    formatCollageStateType(cellValue) {
      if (cellValue == 1) {
        return '';
      } else if (cellValue == 2) {
        return 'success';
      } else if (cellValue == 3) {
        return 'success';
      } else if (cellValue == 4) {
        return 'info';
      }
    },
    //退款状态：(1.待退款 2.已退款 3.待退差价 4.已退差价)
    formatRefundState(cellValue) {
      if (cellValue == 1) {
        return '待退款';
      } else if (cellValue == 2) {
        return '已退款';
      } else if (cellValue == 3) {
        return '待退差价';
      } else if (cellValue == 4) {
        return '已退差价';
      }
    },
    formatRefundStateType(cellValue) {
      if (cellValue == 1) {
        return 'info';
      } else if (cellValue == 2) {
        return 'success';
      } else if (cellValue == 3) {
        return 'info';
      } else if (cellValue == 4) {
        return 'success';
      }
    },
    //支付状态：(1.未支付 5.已付订金 6.已付尾款 7.已付款 11.待退差价 12.已退差价 14.待退款 15.已退款 20.已取消)
    formatPayState(cellValue) {
      if (cellValue == 1) {
        return '未支付';
      } else if (cellValue == 5) {
        return '已付订金';
      } else if (cellValue == 6) {
        return '已付尾款';
      } else if (cellValue == 7) {
        return '已付款';
      } else if (cellValue == 11) {
        return '待退差价';
      } else if (cellValue == 12) {
        return '已退差价';
      } else if (cellValue == 14) {
        return '待退款';
      } else if (cellValue == 15) {
        return '已退款';
      } else if (cellValue == 20) {
        return '已取消';
      }
    },
    formatPayStateType(cellValue) {
      if (cellValue == 1) {
        return 'info';
      } else if (cellValue == 5) {
        return 'success';
      } else if (cellValue == 6) {
        return 'success';
      } else if (cellValue == 7) {
        return 'success';
      } else if (cellValue == 11) {
        return 'warning';
      } else if (cellValue == 12) {
        return 'success';
      } else if (cellValue == 14) {
        return 'warning';
      } else if (cellValue == 15) {
        return 'success';
      } else if (cellValue == 20) {
        return 'danger';
      }
    },
    formatIsMyself(row, column, cellValue) {
      if (cellValue == 0) {
        return '否';
      } else if (cellValue == 1) {
        return '是';
      }
    },
    formatState(row, column, cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      }
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    getHospitalId(val) {
      this.searchForm.hospitalId = val;
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    handleClickAdd() {
      this.$router.push('/sample/collageAdd');
    },
    handleClickEditor(row) {
      this.$router.push({
        path: '/sample/collageEdit',
        query: { id: row.collageId },
      });
    },
    handleClickDelete(row, state) {
      let msg = message.MSG_CONFIRM_DELETE;
      if (state == 0) {
        state = row.state == 0 ? 1 : 0;
        msg = row.state == 0 ? message.MSG_CONFIRM_ENABLE : message.MSG_CONFIRM_DISABLE;
      }
      this.$confirm(msg, message.TITLE_PROMPT, {}).then(() => {
        let data = {
          id: row.collageId,
          state: state,
        };
        CollageApi.deleteData(data).then((res) => {
          this.listTable();
          this.$Utils.notifySuccess();
        });
      });
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/sample/CollageDetail',
        query: { id: row.collageId },
      });
    },
    handleClickViewPayer(row) {
      let that = this;
      that.hideDialogShow = true;
      that.collagePayerList = [];
      let data = {
        collageId: row.collageId,
      };
      CollageApi.listCollagePayer(data)
        .then((res) => {
          that.collagePayerList = res.data;
        })
        .catch((error) => {
          console.error(error);
        });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.HospitalSelect.hospitalIdEmpty();
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        collageId: this.searchForm.collageId,
        hospitalId: this.searchForm.hospitalId,
        linkMan: this.searchForm.linkMan,
        linkPhone: this.searchForm.linkPhone,
        refundState: this.searchForm.refundState,
        collageState: this.searchForm.collageState,
      };
      CollageApi.pageData(data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
